package io.github.loan.common.util;

/**
 * 分页的工具类
 * @author Andone
 *
 */
public class PageInfoUtil {
	/*每页多少条*/
	private int pageSize=5;
	/*总记录数*/
	private int totalRecord;
	
	/*当前页*/
	private int currentPage;
	/*上一页*/
	private int prePage;
	/*下一页*/
	private int nextPage;
	/*总页数*/
	private int totalPage;
	/*当前页 的第几条*/
	private int currentRecord;
	
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public int getTotalRecord() {
		return totalRecord;
	}
	public void setTotalRecord(int totalRecord) {
		this.totalRecord = totalRecord;
	}
	
	public int getCurrentPage() {
		/*如果当前页小于1,默认为1*/
		if(this.currentPage<1){
			this.currentPage=1;
		}
		/**
		 * 如果当前页大于总页数,默认为总页数
		 * &&this.getTotalRecord()>0
		 * 
		 * 如果总记录树为0,那么总页数也为0
		 * 程序执行至此,currentPage肯定>=1
		 * this.currentPage>this.getTotalPage() 肯定成立
		 */
		if(this.currentPage>this.getTotalPage()&&this.getTotalRecord()>0){
			this.currentPage=this.getTotalPage();
		}
		return currentPage;
	}
	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}
	public int getPrePage() {
		this.prePage=this.currentPage-1;
		/*如果当前页小于1,默认为1*/
		if(this.prePage<1){
			this.prePage=1;
		}
		/*如果当前页大于总页数,默认为总页数*/
		if(this.prePage>this.getTotalPage()&&this.getTotalRecord()>0){
			this.prePage=this.getTotalPage();
		}
		return prePage;
	}
	public int getNextPage() {
		this.nextPage=this.currentPage+1;
		if(this.nextPage<1){
			this.nextPage=1;
		}
		if(this.nextPage>this.getTotalPage()){
			this.nextPage=this.getTotalPage();
		}
		if(this.getTotalRecord()==0){
			this.nextPage=1;
		}
		return nextPage;
	}
	public int getTotalPage() {
		return (int)Math.ceil(this.getTotalRecord()*1.0/this.pageSize);
	}
	public int getCurrentRecord() {
		/**
		 * 共35条 ,每页10条
		 * 第一页1
		 * 第二页11
		 * 
		 */
		
		return (this.getCurrentPage()-1)*pageSize;
	}
	@Override
	public String toString() {
		return "PageInfoUtil [pageSize=" + pageSize + ", totalRecord=" + totalRecord + ", currentPage=" + this.getCurrentPage()
		+ ", prePage=" + this.getPrePage() + ", nextPage=" + this.getNextPage() + ", totalPage=" + this.getTotalPage() + ", currRecord="
		+ this.getCurrentRecord() + "]";
	}
	
	/**
	 * 测试
	 * @param args
	 */
	/*public static void main(String[] args) {
		PageInfoUtil pageInfoUtil=new PageInfoUtil();
		pageInfoUtil.setTotalRecord(35);
		pageInfoUtil.setPageSize(10);
		pageInfoUtil.setCurrentPage(4);
		System.out.println(pageInfoUtil);
	}*/
	
	
}
